<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Installing Spatialite &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../index.html" />
    <link rel="up" title="GeoDjango Installation" href="index.html" />
    <link rel="next" title="GeoDjango Model API" href="../model-api.html" />
    <link rel="prev" title="Installing PostGIS" href="postgis.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="postgis.html" title="Installing PostGIS">previous</a>
     |
    <a href="../../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="../model-api.html" title="GeoDjango Model API">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-gis-install-spatialite">
            
  <div class="section" id="s-installing-spatialite">
<span id="s-spatialite"></span><span id="installing-spatialite"></span><span id="spatialite"></span><h1>Installing Spatialite<a class="headerlink" href="#installing-spatialite" title="Permalink to this headline">¶</a></h1>
<p><a class="reference external" href="https://www.gaia-gis.it/fossil/libspatialite">SpatiaLite</a> adds spatial support to SQLite, turning it into a full-featured
spatial database.</p>
<p>Check first if you can install Spatialite from system packages or binaries. For
example, on Debian-based distributions, try to install the <tt class="docutils literal"><span class="pre">spatialite-bin</span></tt>
package. For Mac OS X, follow the
<a class="reference internal" href="#spatialite-macosx"><em>specific instructions below</em></a>. For Windows, you may
find binaries on <a class="reference external" href="http://www.gaia-gis.it/gaia-sins/">Gaia-SINS</a> home page. In any case, you should always
be able to <a class="reference internal" href="#spatialite-source"><em>install from source</em></a>.</p>
<p>When you are done with the installation process, skip to <a class="reference internal" href="#create-spatialite-db"><em>Creating a spatial database for SpatiaLite</em></a>.</p>
<div class="section" id="s-installing-from-source">
<span id="s-spatialite-source"></span><span id="installing-from-source"></span><span id="spatialite-source"></span><h2>Installing from source<a class="headerlink" href="#installing-from-source" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="geolibs.html#geospatial-libs"><em>GEOS and PROJ.4</em></a> should be installed prior to building
SpatiaLite.</p>
<div class="section" id="s-sqlite">
<span id="sqlite"></span><h3>SQLite<a class="headerlink" href="#sqlite" title="Permalink to this headline">¶</a></h3>
<p>Check first if SQLite is compiled with the <a class="reference external" href="http://www.sqlite.org/rtree.html">R*Tree module</a>. Run the sqlite3
command line interface and enter the following query:</p>
<div class="highlight-python"><div class="highlight"><pre>sqlite&gt; CREATE VIRTUAL TABLE testrtree USING rtree(id,minX,maxX,minY,maxY);
</pre></div>
</div>
<p>If you obtain an error, you will have to recompile SQLite from source. Otherwise,
just skip this section.</p>
<p>To install from sources, download the latest amalgamation source archive from
the <a class="reference external" href="http://www.sqlite.org/download.html">SQLite download page</a>, and extract:</p>
<div class="highlight-python"><div class="highlight"><pre>$ wget http://sqlite.org/sqlite-amalgamation-3.6.23.1.tar.gz
$ tar xzf sqlite-amalgamation-3.6.23.1.tar.gz
$ cd sqlite-3.6.23.1
</pre></div>
</div>
<p>Next, run the <tt class="docutils literal"><span class="pre">configure</span></tt> script &#8211; however the <tt class="docutils literal"><span class="pre">CFLAGS</span></tt> environment variable
needs to be customized so that SQLite knows to build the R*Tree module:</p>
<div class="highlight-python"><div class="highlight"><pre>$ CFLAGS=&quot;-DSQLITE_ENABLE_RTREE=1&quot; ./configure
$ make
$ sudo make install
$ cd ..
</pre></div>
</div>
</div>
<div class="section" id="s-spatialite-library-libspatialite-and-tools-spatialite">
<span id="s-spatialitebuild"></span><span id="spatialite-library-libspatialite-and-tools-spatialite"></span><span id="spatialitebuild"></span><h3>SpatiaLite library (<tt class="docutils literal"><span class="pre">libspatialite</span></tt>) and tools (<tt class="docutils literal"><span class="pre">spatialite</span></tt>)<a class="headerlink" href="#spatialite-library-libspatialite-and-tools-spatialite" title="Permalink to this headline">¶</a></h3>
<p>Get the latest SpatiaLite library source and tools bundle from the
<a class="reference external" href="http://www.gaia-gis.it/gaia-sins/libspatialite-sources/">download page</a>:</p>
<div class="highlight-python"><div class="highlight"><pre>$ wget http://www.gaia-gis.it/gaia-sins/libspatialite-sources/libspatialite-amalgamation-2.4.0-5.tar.gz
$ wget http://www.gaia-gis.it/gaia-sins/spatialite-tools-sources/spatialite-tools-2.4.0-5.tar.gz
$ tar xzf libspatialite-amalgamation-2.4.0-5.tar.gz
$ tar xzf spatialite-tools-2.4.0-5.tar.gz
</pre></div>
</div>
<p>Prior to attempting to build, please read the important notes below to see if
customization of the <tt class="docutils literal"><span class="pre">configure</span></tt> command is necessary.  If not, then run the
<tt class="docutils literal"><span class="pre">configure</span></tt> script, make, and install for the SpatiaLite library:</p>
<div class="highlight-python"><div class="highlight"><pre>$ cd libspatialite-amalgamation-2.3.1
$ ./configure # May need to be modified, see notes below.
$ make
$ sudo make install
$ cd ..
</pre></div>
</div>
<p id="spatialite-tools">Finally, do the same for the SpatiaLite tools:</p>
<div class="highlight-python"><div class="highlight"><pre>$ cd spatialite-tools-2.3.1
$ ./configure # May need to be modified, see notes below.
$ make
$ sudo make install
$ cd ..
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>If you&#8217;ve installed GEOS and PROJ.4 from binary packages, you will have to specify
their paths when running the <tt class="docutils literal"><span class="pre">configure</span></tt> scripts for <em>both</em> the library and the
tools (the configure scripts look, by default, in <tt class="docutils literal"><span class="pre">/usr/local</span></tt>).  For example,
on Debian/Ubuntu distributions that have GEOS and PROJ.4 packages, the command would be:</p>
<div class="last highlight-python"><div class="highlight"><pre>$ ./configure --with-proj-include=/usr/include --with-proj-lib=/usr/lib --with-geos-include=/usr/include --with-geos-lib=/usr/lib
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>For Mac OS X users building from source, the SpatiaLite library <em>and</em> tools
need to have their <tt class="docutils literal"><span class="pre">target</span></tt> configured:</p>
<div class="last highlight-python"><div class="highlight"><pre>$ ./configure --target=macosx
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-pysqlite2">
<span id="s-id6"></span><span id="pysqlite2"></span><span id="id6"></span><h3>pysqlite2<a class="headerlink" href="#pysqlite2" title="Permalink to this headline">¶</a></h3>
<p>If you&#8217;ve decided to use a <a class="reference internal" href="../../../databases.html#using-newer-versions-of-pysqlite"><em>newer version of pysqlite2</em></a> instead of the <tt class="docutils literal"><span class="pre">sqlite3</span></tt> Python stdlib
module, then you need to make sure it can load external extensions (i.e. the
required <tt class="docutils literal"><span class="pre">enable_load_extension</span></tt> method is available so <tt class="docutils literal"><span class="pre">SpatiaLite</span></tt> can be
loaded).</p>
<p>This might involve building it yourself. For this, download pysqlite2 2.6, and
untar:</p>
<div class="highlight-python"><div class="highlight"><pre>$ wget https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.6.3.tar.gz
$ tar xzf pysqlite-2.6.3.tar.gz
$ cd pysqlite-2.6.3
</pre></div>
</div>
<p>Next, use a text editor to edit the <tt class="docutils literal"><span class="pre">setup.cfg</span></tt> file to look like the
following:</p>
<div class="highlight-ini"><div class="highlight"><pre><span class="k">[build_ext]</span>
<span class="c1">#define=</span>
<span class="na">include_dirs</span><span class="o">=</span><span class="s">/usr/local/include</span>
<span class="na">library_dirs</span><span class="o">=</span><span class="s">/usr/local/lib</span>
<span class="na">libraries</span><span class="o">=</span><span class="s">sqlite3</span>
<span class="c1">#define=SQLITE_OMIT_LOAD_EXTENSION</span>
</pre></div>
</div>
<p>or if you are on Mac OS X:</p>
<div class="highlight-ini"><div class="highlight"><pre><span class="k">[build_ext]</span>
<span class="c1">#define=</span>
<span class="na">include_dirs</span><span class="o">=</span><span class="s">/Library/Frameworks/SQLite3.framework/unix/include</span>
<span class="na">library_dirs</span><span class="o">=</span><span class="s">/Library/Frameworks/SQLite3.framework/unix/lib</span>
<span class="na">libraries</span><span class="o">=</span><span class="s">sqlite3</span>
<span class="c1">#define=SQLITE_OMIT_LOAD_EXTENSION</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The important thing here is to make sure you comment out the
<tt class="docutils literal"><span class="pre">define=SQLITE_OMIT_LOAD_EXTENSION</span></tt> flag and that the <tt class="docutils literal"><span class="pre">include_dirs</span></tt>
and <tt class="docutils literal"><span class="pre">library_dirs</span></tt> settings are uncommented and set to the appropriate
path if the SQLite header files and libraries are not in <tt class="docutils literal"><span class="pre">/usr/include</span></tt>
and <tt class="docutils literal"><span class="pre">/usr/lib</span></tt>, respectively.</p>
</div>
<p>After modifying <tt class="docutils literal"><span class="pre">setup.cfg</span></tt> appropriately, then run the <tt class="docutils literal"><span class="pre">setup.py</span></tt> script
to build and install:</p>
<div class="highlight-python"><div class="highlight"><pre>$ sudo python setup.py install
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-mac-os-x-specific-instructions">
<span id="s-spatialite-macosx"></span><span id="mac-os-x-specific-instructions"></span><span id="spatialite-macosx"></span><h2>Mac OS X-specific instructions<a class="headerlink" href="#mac-os-x-specific-instructions" title="Permalink to this headline">¶</a></h2>
<p>To install the SpatiaLite library and tools, Mac OS X users can choose between
<a class="reference internal" href="index.html#kyngchaos"><em>KyngChaos packages</em></a> and <a class="reference external" href="http://brew.sh/">Homebrew</a>.</p>
<div class="section" id="s-kyngchaos">
<span id="kyngchaos"></span><h3>KyngChaos<a class="headerlink" href="#kyngchaos" title="Permalink to this headline">¶</a></h3>
<p>First, follow the instructions in the <a class="reference internal" href="index.html#kyngchaos"><em>KyngChaos packages</em></a> section.</p>
<p>When <a class="reference internal" href="#create-spatialite-db"><em>Creating a spatial database for SpatiaLite</em></a>, the <tt class="docutils literal"><span class="pre">spatialite</span></tt> program is required.
However, instead of attempting to compile the SpatiaLite tools from source,
download the <a class="reference external" href="http://www.gaia-gis.it/spatialite-2.3.1/binaries.html">SpatiaLite Binaries</a> for OS X, and install <tt class="docutils literal"><span class="pre">spatialite</span></tt> in a
location available in your <tt class="docutils literal"><span class="pre">PATH</span></tt>.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre>$ curl -O http://www.gaia-gis.it/spatialite/spatialite-tools-osx-x86-2.3.1.tar.gz
$ tar xzf spatialite-tools-osx-x86-2.3.1.tar.gz
$ cd spatialite-tools-osx-x86-2.3.1/bin
$ sudo cp spatialite /Library/Frameworks/SQLite3.framework/Programs
</pre></div>
</div>
<p>Finally, for GeoDjango to be able to find the KyngChaos SpatiaLite library,
add the following to your <tt class="docutils literal"><span class="pre">settings.py</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">SPATIALITE_LIBRARY_PATH</span><span class="o">=</span><span class="s">&#39;/Library/Frameworks/SQLite3.framework/SQLite3&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-homebrew">
<span id="homebrew"></span><h3>Homebrew<a class="headerlink" href="#homebrew" title="Permalink to this headline">¶</a></h3>
<p><a class="reference external" href="http://brew.sh/">Homebrew</a> handles all the SpatiaLite related packages on your behalf,
including SQLite3, SpatiaLite, PROJ, and GEOS. Install them like this:</p>
<div class="highlight-python"><div class="highlight"><pre>$ brew update
$ brew install spatialite-tools
$ brew install gdal
</pre></div>
</div>
<p>Finally, for GeoDjango to be able to find the SpatiaLite library, add the
following to your <tt class="docutils literal"><span class="pre">settings.py</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">SPATIALITE_LIBRARY_PATH</span><span class="o">=</span><span class="s">&#39;/usr/local/lib/mod_spatialite.dylib&#39;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-creating-a-spatial-database-for-spatialite">
<span id="s-create-spatialite-db"></span><span id="creating-a-spatial-database-for-spatialite"></span><span id="create-spatialite-db"></span><h2>Creating a spatial database for SpatiaLite<a class="headerlink" href="#creating-a-spatial-database-for-spatialite" title="Permalink to this headline">¶</a></h2>
<p>After you&#8217;ve installed SpatiaLite, you&#8217;ll need to create a number of spatial
metadata tables in your database in order to perform spatial queries.</p>
<p>If you&#8217;re using SpatiaLite 2.4 or newer, use the <tt class="docutils literal"><span class="pre">spatialite</span></tt> utility to
call the <tt class="docutils literal"><span class="pre">InitSpatialMetaData()</span></tt> function, like this:</p>
<div class="highlight-python"><div class="highlight"><pre>$ spatialite geodjango.db &quot;SELECT InitSpatialMetaData();&quot;
the SPATIAL_REF_SYS table already contains some row(s)
 InitSpatiaMetaData ()error:&quot;table spatial_ref_sys already exists&quot;
0
</pre></div>
</div>
<p>You can safely ignore the error messages shown. When you&#8217;ve done this, you can
skip the rest of this section.</p>
<p>If you&#8217;re using SpatiaLite 2.3, you&#8217;ll need to download a
database-initialization file and execute its SQL queries in your database.</p>
<p>First, get it from the <a class="reference external" href="http://www.gaia-gis.it/spatialite-2.3.1/resources.html">SpatiaLite Resources</a> page:</p>
<div class="highlight-python"><div class="highlight"><pre>$ wget http://www.gaia-gis.it/spatialite-2.3.1/init_spatialite-2.3.sql.gz
$ gunzip init_spatialite-2.3.sql.gz
</pre></div>
</div>
<p>Then, use the <tt class="docutils literal"><span class="pre">spatialite</span></tt> command to initialize a spatial database:</p>
<div class="highlight-python"><div class="highlight"><pre>$ spatialite geodjango.db &lt; init_spatialite-2.3.sql
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The parameter <tt class="docutils literal"><span class="pre">geodjango.db</span></tt> is the <em>filename</em> of the SQLite database
you want to use.  Use the same in the <a class="reference internal" href="../../../settings.html#std:setting-DATABASES"><tt class="xref std std-setting docutils literal"><span class="pre">DATABASES</span></tt></a> <tt class="docutils literal"><span class="pre">&quot;name&quot;</span></tt> key
inside your <tt class="docutils literal"><span class="pre">settings.py</span></tt>.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>When running <tt class="docutils literal"><span class="pre">manage.py</span> <span class="pre">migrate</span></tt> with a SQLite (or SpatiaLite) database,
the database file will be automatically created if it doesn&#8217;t exist. In
this case, if your models contain any geometry columns, you&#8217;ll see this
error:</p>
<div class="highlight-python"><div class="highlight"><pre>CreateSpatialIndex() error: &quot;no such table: geometry_columns&quot;
</pre></div>
</div>
<p class="last">It&#8217;s because the table creation queries are executed without spatial
metadata tables. To avoid this, make the database file before executing
<tt class="docutils literal"><span class="pre">manage.py</span> <span class="pre">migrate</span></tt> as described above.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Installing Spatialite</a><ul>
<li><a class="reference internal" href="#installing-from-source">Installing from source</a><ul>
<li><a class="reference internal" href="#sqlite">SQLite</a></li>
<li><a class="reference internal" href="#spatialite-library-libspatialite-and-tools-spatialite">SpatiaLite library (<tt class="docutils literal"><span class="pre">libspatialite</span></tt>) and tools (<tt class="docutils literal"><span class="pre">spatialite</span></tt>)</a></li>
<li><a class="reference internal" href="#pysqlite2">pysqlite2</a></li>
</ul>
</li>
<li><a class="reference internal" href="#mac-os-x-specific-instructions">Mac OS X-specific instructions</a><ul>
<li><a class="reference internal" href="#kyngchaos">KyngChaos</a></li>
<li><a class="reference internal" href="#homebrew">Homebrew</a></li>
</ul>
</li>
<li><a class="reference internal" href="#creating-a-spatial-database-for-spatialite">Creating a spatial database for SpatiaLite</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="postgis.html">Installing PostGIS</a></li>
    
    
      <li>Next: <a href="../model-api.html">GeoDjango Model API</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../index.html">API Reference</a>
        
          <ul><li><a href="../../index.html"><tt class="docutils literal"><span class="pre">contrib</span></tt> packages</a>
        
          <ul><li><a href="../index.html">GeoDjango</a>
        
          <ul><li><a href="index.html">GeoDjango Installation</a>
        
        <ul><li>Installing Spatialite</li></ul>
        </li></ul></li></ul></li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../../../_sources/ref/contrib/gis/install/spatialite.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="postgis.html" title="Installing PostGIS">previous</a>
     |
    <a href="../../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="../model-api.html" title="GeoDjango Model API">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>